/**
 * Copyright (C), 2019-2022, 宇信融汇
 * FileName: ShopCarTest
 * Author:   666
 * Date:     2022/1/12 10:31
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 666           修改时间           版本号              描述
 */
package com.day03.oop;

import java.util.Scanner;

/**
 * @Classname ShopCarTest
 * @Description TODO
 * @Date 2022/1/12 10:31
 * @Created by 666
 */
public class ShopCarTest {

    public static void main(String[] args) {
        Goods[] shopCar = new Goods[100];
        while (true) {
            System.out.println("请选择你的操作");
            System.out.println("输入添加商品add");
            System.out.println("输入查询商品query");
            System.out.println("输入更新商品update");
            System.out.println("输入支付apy");
            Scanner scanner = new Scanner(System.in);
            System.out.println("输入操作");
            String next = scanner.next();
            switch (next) {
                case "add":
                    addGoods(shopCar, scanner);
                    break;
                case "query":
                    queryGoods(shopCar);
                    break;
                case "update":
                    updateGoods(shopCar,scanner);
                    break;
                case "pay":
                    payGoods(shopCar);
                    break;
            }
        }
    }

    private static void payGoods(Goods[] shopCar) {
    }

    private static void updateGoods(Goods[] shopCar, Scanner scanner) {
        while (true) {
            System.out.println("请你输入需要更新的id");
            int id = scanner.nextInt();
            Goods goods = getGoodsById(shopCar,id);
            if(goods==null){
                System.out.println("没有购买商品");
            }else{
                System.out.println("请输入"+goods.name+"商品数量");
                int bugNumber = scanner.nextInt();
                goods.number = bugNumber;
                System.out.println("修改商品数量成功");
                queryGoods(shopCar);
                break;
            }
        }

    }

    public static Goods getGoodsById(Goods[] shopCar,int id) {
        for (int i = 0; i < shopCar.length; i++) {
            Goods goods = shopCar[i];
            if(goods!=null){
                if(goods.id==id){
                    return goods;
                }
            }else{
                return null;
            }
        }
        return null;
    }

    private static void queryGoods(Goods[] shopCar) {
        System.out.println("===============购物车信息如下===============");
        System.out.println("编号\t\t名称\t\t\t数量\t\t\t价格");
        for (int i = 0; i < shopCar.length; i++) {
            if (shopCar[i] != null) {
                Goods good = shopCar[i];
                System.out.println(good.id + "\t\t" + good.name + "\t\t\t" + good.number + "\t\t\t" + good.price);
            } else {
                break;
            }

        }
    }

    private static void addGoods(Goods[] shopCar, Scanner scanner) {
        System.out.println("输入ID");
        int id = scanner.nextInt();
        System.out.println("输入名称");
        String name = scanner.next();
        System.out.println("输入数量");
        int number = scanner.nextInt();
        System.out.println("输入价格");
        double price = scanner.nextDouble();
        Goods goods = new Goods();
        goods.id = id;
        goods.name = name;
        goods.number = number;
        goods.price = price;
        for (int i = 0; i < shopCar.length; i++) {
            if (shopCar[i] == null) {
                shopCar[i] = goods;
                break;
            } else {
                System.out.println("购物车已满，无法添加");
                break;
            }
        }
        System.out.println("完成添加商品");
    }
}
